package msg.services;

import static org.junit.Assert.*;
import msg.*;

import org.apache.catalina.tribes.util.Arrays;
import org.junit.*;

import user.*;

import common.DALTests;
import common.dal.Transaction;

import cs236369.hw5.*;

@SuppressWarnings({"javadoc", "static-method"})
public class MessageFinderTest extends DALTests {
	SearchWS	$	= new SearchWSImpl();
	
	@Before
	public void setupMM() throws Exception {
		MessageManager.getNewInstance(t).clear();
		t.commit();
	}
	
	@Test
	public void testSearch() throws Exception {
		assertTrue(Arrays.equals(new String[0], $.searchLocal(0, 0, 1)));
		t = new Transaction();
		User user = LoginManager.getNewInstance(t).registerUser("gal", "whatever", PASSWORD);
		MessageManager.getNewInstance(t).createNewMessage(new Location(1.1, 0.1), "foo", user);
		t.commit();
		assertEquals("{0.1||1.1||foo}", Arrays.toString($.searchLocal(0.1, 1.1, 0)));
		t = new Transaction();
		MessageManager.getNewInstance(t).createNewMessage(new Location(1.1, 2.1), "bar", user);
		t.commit();
		t = new Transaction();
		assertEquals("{0.1||1.1||foo}", Arrays.toString($.searchLocal(0.2, 1.2, 16000)));
		assertEquals("{0.1||1.1||foo, 2.1||1.1||bar}", Arrays.toString($.searchLocal(1.5, 1.5, 170 * 1000)));
	}
	
	@Test
	public void testEmpty() throws Exception {
		t = new Transaction();
		User user = LoginManager.getNewInstance(t).registerUser("gal2", "whatever", PASSWORD);
		MessageManager.getNewInstance(t).createNewMessage(new Location(0.1, 0.1), "foo", user);
		t.commit();
		assertEquals("{0.1||0.1||foo}", Arrays.toString($.searchLocal(0.1, 0.1, 0)));
		t = new Transaction();
		MessageManager.getNewInstance(t).createNewMessage(new Location(1.1, 1.1), "bar", user);
		t.commit();
		t = new Transaction();
		assertTrue(Arrays.equals(new String[0], $.searchKeywords(new String[] {"*"})));
	}
}
